<?php 
ob_start(); session_start();
require_once('config/setting.php');
if (!$_SESSION['userid'])
{
	header('location: login.php');
	
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<title>The Century - Admin Panel</title>
	<style>
        .topmenu{
            font-family:arial;
			font-weight:bold;
            font-size:16px;
            color:#FFFFFF;
            text-decoration:none;
        }
        .topmenu:hover{
            font-family:arial;
			font-weight:bold;
            font-size:16px;
            color:#FFFFFF;
            text-decoration:underline;
        }
    </style>
</head>
<body>
<table width="100%">
<div style="padding:10px; font-size:20px; font-family:verdana;"><span>THE CENTURY <span style="font-size:14px">Admin Panel</span></span></div>

<tr>
	<td style="width:100%; height:auto; ">
	  <table  style="width:100%; height:100%" border="0">
		<tr>
			<td style="background:#FF0000; height:71px;">
			   <div align="center">	
				   <table style="width:960px;" cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td style="height:42px; padding-top:15px"  >
								<table align="center" cellpadding="0" cellspacing="0" border="0">
									<tr>
										<td style="font-weight:bold; color:#FFFFFF">
                                        	<a href="view-customers.php" class="topmenu">View Customers</a> &nbsp;|&nbsp; 
											<a href="reports.php" class="topmenu">Reports</a> &nbsp;|&nbsp; 
                                            <a href="reservation.php" class="topmenu">Reservation</a> &nbsp;|&nbsp; 
                                            <a href="slots.php" class="topmenu">Slots</a> &nbsp;|&nbsp;  
                                            <a href="question.php" class="topmenu">Question</a> &nbsp;|&nbsp;  
                                            <a href="menus.php" class="topmenu">Menus</a> &nbsp;|&nbsp;  
                                            <a href="logout.php" class="topmenu">Logout</a> 
                                        </td>
									</tr>
								</table>							
                             </td>
						</tr>
					</table>			
				</div>			
              </td>
		</tr>	
	</table></td>
</tr>
<!--content-->
<?php 
	if($_POST['mealsubmit']){
		$warning = '';	
		$meal = $_POST['meal'];
		$price = $_POST['price'];
		$category = $_POST['category'];
		$description = $_POST['description'];
		$datein = time();
		$result = mysql_query("SELECT * FROM tbl_menu where name='$meal' ") or die (mysql_error());
        if($row = mysql_fetch_array($result)){		
			$warning = '<br>Warning! Meal already exists please try again.<br>';
		}else{
			$warning = '<br>Meal successfuly created.</br>';
			$uploadDir = "../uploads/";
			$photofilename = $_POST['picture'];
			
			$fileName = $_FILES['picture']['name'];
			$tmpName  = $_FILES['picture']['tmp_name'];
			
			$ext = substr($fileName, strrpos($fileName, '.') + 1);
			if(strtoupper($ext)=='JPG' or strtoupper($ext)=='GIF'){
				$filePath = $uploadDir . $fileName;
				move_uploaded_file($tmpName, $filePath);
			}
			mysql_query("insert into tbl_menu (id,name,price,category,description,picture,active,dateposted) values ('','$meal','$price','$category','$description','$fileName','1','$datein')") or die (mysql_error());
		}
	}
	
	
    if($_POST['groupmealsubmit']){
		$warning = '';	
		$presult = mysql_query("select * from tbl_menu") or die (mysql_error());
		$smeals = '';
		while ($prow = mysql_fetch_array($presult)){
			$id = $prow['id']; 
			$nmeal = 'm'.$id;
			$smeals .= $_POST[$nmeal].',';
		}
		
		$meal = $_POST['meal'];
		$price = $_POST['price'];
		$datein = time();
		$result = mysql_query("SELECT * FROM tbl_groupmenu where name='$meal' ") or die (mysql_error());
        if($row = mysql_fetch_array($result)){		
			$warning = '<br>Warning! Group Meal already exists please try again.<br>';
		}else{
			$warning = '<br>Group Meal successfuly created.</br>';
			mysql_query("insert into tbl_groupmenu (id,nmeals,name,price,active,dateposted) values ('','$smeals','$meal','$price','1','$datein')") or die (mysql_error());
		}
	}
	
	
    if($_POST['editgroupmeal']){
		$warning = '';	
		$presult = mysql_query("select * from tbl_menu") or die (mysql_error());
		$smeals = '';
		while ($prow = mysql_fetch_array($presult)){
			$id = $prow['id']; 
			$nmeal = 'm'.$id;
			$smeals .= $_POST[$nmeal].',';
		}
		$uid = $_POST['id'];
		$meal = $_POST['meal'];
		$price = $_POST['price'];
		
		mysql_query("update tbl_groupmenu set nmeals='".$smeals."', name='".$meal."', price='".$price."' where id='$uid' limit 1") or die (mysql_error());		
		header("Location: menus.php?action=view group meals");
		$warning = '<br>Group Meal successfuly updated.</br>';
	}


	if($_GET['action']=='delete'){
		$tmpid = $_GET['id'];
		mysql_query("delete from tbl_category where id='$tmpid'");				
		header("Location: menus.php?action=add category");
	}
	
	if($_GET['action']=='delete meal'){
		$tmpid = $_GET['id'];
		mysql_query("update tbl_menu set active='0' where id='$tmpid' limit 1") or die (mysql_error());		
		//mysql_query("delete from tbl_menu where id='$tmpid'");				
		header("Location: menus.php?action=view all meals");
	}
	
	if($_GET['action']=='restore meal'){
		$tmpid = $_GET['id'];
		mysql_query("update tbl_menu set active='1' where id='$tmpid' limit 1") or die (mysql_error());		
		header("Location: menus.php?action=view all meals");
	}
	
	
	if($_GET['action']=='delete group meal'){
		$tmpid = $_GET['id'];
		mysql_query("update tbl_groupmenu set active='0' where id='$tmpid' limit 1") or die (mysql_error());		
		//mysql_query("delete from tbl_groupmenu where id='$tmpid'");				
		header("Location: menus.php?action=view group meals");
	}

	if($_GET['action']=='restore group meal'){
		$tmpid = $_GET['id'];
		mysql_query("update tbl_groupmenu set active='1' where id='$tmpid' limit 1") or die (mysql_error());		
		header("Location: menus.php?action=view group meals");
	}
	
	
	if($_POST['catsubmit']){
		$warning = '';	
		$category = $_POST['category'];
        if(!empty($category)){		
			$warning = '<br>Category successfuly added.</br>';
			mysql_query("insert into tbl_category (id,category) values ('','$category')") or die (mysql_error());
		}
	}
	
	if($_POST['editmeal']){
		$warning = '';	
		$warning = '<br>Category successfuly added.</br>';
		$id = $_POST['id'];
		$meal = $_POST['meal'];
		$price = $_POST['price'];
		$category = $_POST['category'];
		$description = $_POST['description'];
		$uploadDir = "../uploads/";
		$photofilename = $_POST['picture'];
		
		$fileName = $_FILES['picture']['name'];
		$tmpName  = $_FILES['picture']['tmp_name'];
		
		$ext = substr($fileName, strrpos($fileName, '.') + 1);
		if(strtoupper($ext)=='JPG' or strtoupper($ext)=='GIF'){
			$filePath = $uploadDir . $fileName;
			move_uploaded_file($tmpName, $filePath);
		}
		if($fileName){
			mysql_query("update tbl_menu set name='".$meal."', price='".$price."', category='".$category."', description='".$description."' , picture='".$fileName."' where id='$id' limit 1") or die (mysql_error());		
		}else{
			mysql_query("update tbl_menu set name='".$meal."', price='".$price."', category='".$category."', description='".$description."' where id='$id' limit 1") or die (mysql_error());		
		}
		header("Location: menus.php?action=view all meals");
	}
	
	
	function getcat($tmpid){
		$result = mysql_query("select * from tbl_category where id='$tmpid' limit 1" ) or die (mysql_error());
		if ($row = mysql_fetch_array($result)){							
			$catname = $row['category'];
		}
		return $catname;
	}

?>
<tr>
	<td style="width:100%; background:url(../images/w_bgr.jpg); height:500px; font-family:arial; font-size:12px" valign="top" align="center">
    	<form name="mealform" method="post" enctype="multipart/form-data">
    	<div align="center">
        	<input type="button" value="Add Meal" onClick="location.href='menus.php?action=add meal'" style="width:130px; height:40px">
            <input type="button" value="View All Meals" onClick="location.href='menus.php?action=view all meals'" style="width:130px; height:40px">
        	<input type="button" value="Add Group Meal" onClick="location.href='menus.php?action=add group meal'" style="width:130px; height:40px">
            <input type="button" value="View All Group Meals" onClick="location.href='menus.php?action=view group meals'" style="width:150px; height:40px">
            <input type="button" value="Add Category" onClick="location.href='menus.php?action=add category'" style="width:130px; height:40px">
            <br><br>
        </div>
        <div style="width:600px;"  align="left">
        	<? if($_GET['action'] == 'add category'){ ?>
            	<span style="font-family:arial; font-size:18px; color:#F00"><b>Add Category</b></span><br><br>
            	Category<br>
                <input name="category"  type="text" style="width:500px; margin-bottom:10px"><br>
                <input name="catsubmit" type="submit" value="Submit" style="width:150px; height:40px"><br><br>
                <table cellpadding="0" cellspacing="0" width="400px" style="border:solid 1px #CCCCCC; font-size:12px">
                	<tr>
                    	<td width="102" height="30px" align="center" bgcolor="#999999" style="border-right:solid 1px #cccccc; color:#FFF"><b>Action</b></td>
                        <td width="296" bgcolor="#999999" style="padding-left:10px; color:#FFF"><b>Category</b></td>
                    </tr>
                    <?php 
						$result = mysql_query("select * from tbl_category") or die (mysql_error());
						while ($row = mysql_fetch_array($result)){
							$id = $row['id'];
							$category = $row['category'];
							echo '<tr>
									<td width="102" height="26px" align="center" style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC"><a href="?action=add category&action=delete&id='.$id.'" onclick="return confirmation();">Remove</a></td>
									<td width="296" style="padding-left:10px; border-bottom:dashed 1px #CCCCCC">'.$category.'</td>
								  </tr>';
						}
					?>	
                </table>
            <? } ?>
        
        
        	<? if($_GET['action'] == 'add meal'){ ?>
            	<span style="font-family:arial; font-size:18px; color:#F00"><b>Add Meal</b></span><br><br>
            	Name<br>
                <input name="meal"  type="text" style="width:500px; margin-bottom:10px"><br>
            	Price<br>
                <input name="price"  type="text" style="margin-bottom:10px"><br>
                Picture<br>
                <input name="picture"  type="file" style="margin-bottom:10px"> (<i>Picture in JGP and GIF format are only accepted</i>)<br>
            	Category<br>
                <select name="category">
						 <option selected value=""> - Select Category - </option>	
                        <?
                        $cresult = mysql_query("select * from tbl_category ") or die (mysql_error());
                        $crow = mysql_num_rows($cresult); 
                        while ($crow = mysql_fetch_array($cresult)){
                            $id = $crow['id'];
                            $category = $crow['category'];
                            echo '<option value="'.$id.'">'.$category.'</option>';	
                         }	?>                	
                </select><br><br>   
            	Description<br>
                <textarea name="description" style="width:500px; height:100px; margin-bottom:10px"></textarea><br>
                <span style="color:#F00"><?=$warning?></span><br>
                <input name="mealsubmit" type="submit" value="Submit" style="width:150px; height:40px">
            <? } ?>
            
        	<? if($_GET['action'] == 'edit meal'){ 
					$tmpid = $_GET['id'];
					$result = mysql_query("select * from tbl_menu where id='$tmpid'") or die (mysql_error());
					if ($row = mysql_fetch_array($result)){
						$id = $row['id'];
						$meal = $row['name'];
						$price = $row['price'];
						$category = $row['category'];
						$picture = $row['picture'];
						$description = $row['description'];
						if($picture){
							$npict = '../uploads/'.$picture;	
						}else{
							$npict = '../images/no-photo.png';
						}
					}
			?>
            	<input type="hidden" name="id" value="<?=$tmpid?>">
            	<span style="font-family:arial; font-size:18px; color:#F00"><b>Add Meal</b></span><br><br>
            	Name<br>
                <input name="meal"  type="text" value="<?=$meal;?>" style="width:500px; margin-bottom:10px"><br>
            	Price<br>
                <input name="price"  type="text" value="<?=$price;?>" style="margin-bottom:10px"><br>
                Picture<br>
                <img src="<?=$npict?>" width="275px" height="170px" style="border:dotted 1px #999999; padding:10px; " /><br><br>
                Replace New Picture here (<i>Note: Please don't select file if you don't want to change the picture. </i>)<br>
                <input name="picture"  type="file" style="margin-bottom:10px"> (<i>Picture in JGP and GIF format are only accepted</i>)<br>
            	Category<br>
                <select name="category">
                        <?
                        $cresult = mysql_query("select * from tbl_category ") or die (mysql_error());
                        $crow = mysql_num_rows($cresult); 
                        while ($crow = mysql_fetch_array($cresult)){
                            $id = $crow['id'];
                            $category = $crow['category'];
							if($category==$id){
	                            echo '<option value="'.$id.'" selected>'.$category.'</option>';	
							}else{
								echo '<option value="'.$id.'">'.$category.'</option>';		
							}
                         }	?>                	
                </select>  <br><br>
            	Description<br>
                <textarea name="description" style="width:500px; height:100px; margin-bottom:10px"><?=$description;?></textarea><br>
                <span style="color:#F00"><?=$warning?></span><br>
                <input name="editmeal" type="submit" value="Update" style="width:150px; height:40px">
            <? } ?>
            
            
        	<? if($_GET['action'] == 'add group meal'){ ?>
            	<span style="font-family:arial; font-size:18px; color:#F00"><b>Add Group Meal</b></span><br><br>
            	Group Meal Name<br>
                <input name="meal"  type="text" value="" style="width:500px; margin-bottom:10px"><br>
            	Price<br>
                <input name="price"  type="text" value="" style="margin-bottom:10px"><br>
            	Select Meals<br>
                <?php
					$result = mysql_query("select * from tbl_menu where active='1'") or die (mysql_error());
					while ($row = mysql_fetch_array($result)){
						$id = $row['id']; 
						$name = $row['name'];                 
						$price = $row['price'];                 
						$nmeal = 'm'.$id;
						echo '<input name="m'.$id.'" id="m'.$id.'" type="checkbox" value="" onclick="selectmeal(\''.$nmeal.'\',\''.$price.'\',\''.$id.'\')"> '.$name.' -  Php '.$price.'<br>';
					}
                ?>
                <br>
                <span style="color:#F00"><?=$warning?></span><br>
                <input name="groupmealsubmit" type="submit" value="Submit" style="width:150px; height:40px">
            <? } ?>
            
            
        	<? if($_GET['action'] == 'edit group meal'){ 
					$tmpid = $_GET['id'];
					$result = mysql_query("select * from tbl_groupmenu where id='$tmpid'") or die (mysql_error());
					if ($row = mysql_fetch_array($result)){
						$id = $row['id'];
						$meal = $row['name'];
						$price = $row['price'];
						$description = $row['description'];
						$allmeals = $row['nmeals'];
					}			

		   ?>
            	<span style="font-family:arial; font-size:18px; color:#F00"><b>Edit Group Meal</b></span><br><br>
                <input type="hidden" name="id" value="<?=$tmpid?>">
            	Group Meal Name<br>
                <input name="meal"  type="text" value="<?=$meal?>" style="width:500px; margin-bottom:10px"><br>
            	Price<br>
                <input name="price"  type="text" value="<?=$price?>" style="margin-bottom:10px"><br>
            	Select Meals<br>
                <?php
					$result = mysql_query("select * from tbl_menu where active='1'") or die (mysql_error());
					while ($row = mysql_fetch_array($result)){
						$id = $row['id']; 
						$name = $row['name'];                 
						$price = $row['price'];                 
						$cat = $row['category'];
						$nmeal = 'm'.$id;
						$checkmeal = explode(',',$allmeals);
						$exist = '0';
						for ($i=0;$i<count($checkmeal);$i++){
							if($checkmeal[$i]==$id){
								$exist = '1';	
							}
						}
						if($exist=='1'){
							echo '<input name="m'.$id.'" id="m'.$id.'" type="checkbox" value="'.$id.'" checked onclick="selectmeal(\''.$nmeal.'\',\''.$price.'\',\''.$id.'\')"> '.$name.' -  Php '.$price.' ('.getcat($cat).')<br>';
						}else{
							echo '<input name="m'.$id.'" id="m'.$id.'" type="checkbox" value="" onclick="selectmeal(\''.$nmeal.'\',\''.$price.'\',\''.$id.'\')"> '.$name.' -  Php '.$price.' ('.getcat($cat).')<br>';
						}
					}
                ?>
                <br>
                <span style="color:#F00"><?=$warning?></span><br>
                <input name="editgroupmeal" type="submit" value="Update" style="width:150px; height:40px" >
            <? } ?>
            
            
            <? if($_GET['action'] == 'view all meals'){ ?>
            	<table cellpadding="0" cellspacing="0" width="700px" style="font-size:12px; border:solid 1px #CCCCCC; "  >
                	<tr>
                    	<td width="149" height="30px" align="center" bgcolor="#999999" style="border-right:solid 1px #cccccc; color:#FFF"><b>Action</b></td>
                        <td width="221" align="center" bgcolor="#999999" style="color:#FFFFFF; border-right:solid 1px #cccccc;" ><b>Menu</b></td>
                        <td width="134" align="center" bgcolor="#999999" style="color:#FFFFFF; border-right:solid 1px #cccccc;"><b>Category</b></td>
                        <td width="94" align="center" bgcolor="#999999" style="color:#FFFFFF"><b>Price</b></td>
                    </tr>
                    <?php 
						$result = mysql_query("select * from tbl_menu") or die (mysql_error());
						while ($row = mysql_fetch_array($result)){
							$id = $row['id'];
							$name = $row['name'];
							$price = $row['price'];
							$catid = $row['category'];
							$active = $row['active'];
							$catresult = mysql_query("select * from tbl_category where id='$catid' limit 1") or die (mysql_error());
							$category = '';
							if ($catrow = mysql_fetch_array($catresult)){
								$category = $catrow['category'];
							}
							if($active=='1'){
							echo '<tr>
									<td height="26px" style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC" align="center" ><a href="?id='.$id.'&action=delete meal" onclick="return confirmation();">Remove</a> | <a href="?id='.$id.'&action=edit meal">Edit</a></td>
									<td style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC; padding-left:10px" align="left" >'.$name.'</td>
									<td style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC; padding-left:10px" align="left" >'.$category.'</td>
									<td style="border-bottom:dashed 1px #CCCCCC; padding-left:10px" align="left" >'.$price.'</td>
								</tr>';
							}
							if($active=='0'){
							echo '<tr>
									<td height="26px" style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC" align="center" ><a href="?id='.$id.'&action=restore meal" >Restore</a> | <a href="?id='.$id.'&action=edit meal">Edit</a></td>
									<td style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC; color:#999999; padding-left:10px; text-decoration:line-through" align="left" >'.$name.'</td>
									<td style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC; color:#999999; padding-left:10px; text-decoration:line-through" align="left" >'.$category.'</td>
									<td style="border-bottom:dashed 1px #CCCCCC; padding-left:10px; color:#999999; text-decoration:line-through" align="left" >'.$price.'</td>
								</tr>';
							}
							
						}
                    ?>
                </table>
					            
            <? } ?>
            
            <? if($_GET['action'] == 'view group meals'){ ?>
            	<table cellpadding="0" cellspacing="0" width="700px" style="font-size:12px; border:solid 1px #CCCCCC; "  >
                	<tr>
                    	<td width="149" height="30px" align="center" bgcolor="#999999" style="border-right:solid 1px #cccccc; color:#FFF"><b>Action</b></td>
                        <td width="221" align="center" bgcolor="#999999" style="color:#FFFFFF; border-right:solid 1px #cccccc; " ><b>Group Menu</b></td>
                        <td width="134" align="center" bgcolor="#999999" style="color:#FFFFFF; border-right:solid 1px #cccccc; "><b>Consists of</b></td>
                        <td width="94" align="center" bgcolor="#999999" style="color:#FFFFFF"><b>Price</b></td>
                    </tr>
                    <?php 
						$result = mysql_query("select * from tbl_groupmenu") or die (mysql_error());
						while ($row = mysql_fetch_array($result)){
							$id = $row['id'];
							$name = $row['name'];
							$price = $row['price'];
							$active = $row['active'];
							$smeals = $row['nmeals'];
							$arr_meal = explode(',', $smeals);
							$mealno = count($arr_meal);
							$nmeals= '';
							for($i=0;$i<$mealno;$i++){
								$mealid = $arr_meal[$i];
								$mresult = mysql_query("select * from tbl_menu where id='$mealid' limit 1") or die (mysql_error());
								if ($mrow = mysql_fetch_array($mresult)){
									$nmeals .= $mrow['name'].', ';
								}
							}
							if($active=='1'){
								echo '<tr>
										<td height="26px" style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC" align="center" ><a href="?id='.$id.'&action=delete group meal" onclick="return confirmation();">Remove</a> | <a href="?id='.$id.'&action=edit group meal">Edit</a></td>
										<td style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC; padding-left:10px" align="left" >'.$name.'</td>
										<td style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC; padding-left:10px" align="left" >'.$nmeals.'</td>
										<td style="border-bottom:dashed 1px #CCCCCC; padding-left:10px" align="left" >'.$price.'</td>
									</tr>';
							}
							if($active=='0'){
								echo '<tr>
										<td height="26px" style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC" align="center" ><a href="?id='.$id.'&action=restore group meal" >Restore</a> | <a href="?id='.$id.'&action=edit group meal">Edit</a></td>
										<td style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC; color:#999999; padding-left:10px; text-decoration:line-through" align="left" >'.$name.'</td>
										<td style="border-right:solid 1px #cccccc; border-bottom:dashed 1px #CCCCCC; color:#999999; padding-left:10px; text-decoration:line-through" align="left" >'.$nmeals.'</td>
										<td style="border-bottom:dashed 1px #CCCCCC; color:#999999; padding-left:10px; text-decoration:line-through" align="left" >'.$price.'</td>
									</tr>';
							}
							
						}
                    ?>
                </table>
					            
            <? } ?>
            
            
        </div>
        </form>
     </td>
</tr>
<!--footer-->
<tr>
	<td style="width:100%;"><table style="width:100%; height:100%">
		<tr>
			<td style="width:50%;"><img alt="" src="../images/spacer.gif" width="1" height="1"></td>
			<td style="width:766px;"><table style="width:766px;">
				<tr><td class="end"><table>
					
				</table></td></tr>
				<tr>
				  <td class="end">&nbsp;</td>
			  </tr>
			</table></td>
			<td style="width:50%;"><img alt="" src="../images/spacer.gif" width="1" height="1"></td>
		</tr>
	</table></td>
</tr>
</table>
<script>
	function confirmation(){
		var answer=confirm("Are you sure you want to remove this item?")
		if(answer){
			return true
		}else{
			return false
		}
	}
	
	function selectmeal(clk,price,nid){
		var nclk = document.getElementById(clk);
		if(nclk.checked==true){
			nclk.value=nid;
		}else{	
			nlck.value='';
		}
	}	
</script>

</body>
</html>
